/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.session.CNFUse;
import controller.ISessionManager;

/**
 * 
 * A dialog to export a CNF to an other file.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGExportCNFDialog extends JDialog implements ActionListener, KeyListener {

	private static final long	serialVersionUID		= 1L;
	public static final String	TITLE					= "Exportation_dialog";
	public static final String	EXPORT_MESSAGE			= "EXPORT";
	public static final String	DEFAULT_OUT_DIRECTORY	= System.getProperty("user.dir") + "/cnf/";	;
	public static final String	OUT_TITLE				= "Output";
	public static final String	OUT_DESCRIPTION			= "Output file: ";
	public static final String	DIRECTORY_BUTTON		= "Search directory";
	public static final String	PARAMETER_TITLE			= "Parameters";

	private ISessionManager		manager;
	private CNFUse				cnf;
	private JButton				run;
	private JPanel				panel;


	protected JPanel			outPanel;
	protected JLabel			outDescritpion;
	protected JTextField		outField;
	protected JButton			outButton;
	protected static File		lastDirectory			= new File(DEFAULT_OUT_DIRECTORY);


	/**
	 * Create {@link IGExportCNFDialog}.
	 * 
	 * @param manager
	 *            {@link ISessionManager} of the session
	 * @param cnf
	 *            {@link CNFUse} concern by the export
	 * @param owner
	 *            window owner of the dialog
	 */
	public IGExportCNFDialog(ISessionManager manager, CNFUse cnf, Window owner) {
		super(owner, TITLE);

		this.manager = manager;
		this.cnf = cnf;

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		setContentPane(panel);

		// out
		outPanel = new JPanel(new GridLayout(2, 1));

		JPanel tmp = new JPanel(new GridLayout(1, 3));
		outDescritpion = new JLabel(OUT_DESCRIPTION);
		tmp.add(outDescritpion);
		outButton = new JButton(DIRECTORY_BUTTON);
		outButton.addActionListener(this);
		tmp.add(new JLabel());
		tmp.add(outButton);

		outPanel.add(tmp);

		outField = new JTextField(lastDirectory.getAbsolutePath() + "/");
		outPanel.add(outField);
		outPanel.setBorder(BorderFactory.createTitledBorder(PARAMETER_TITLE));


		panel.add(outPanel);


		// bouton
		run = new JButton(EXPORT_MESSAGE);
		// run.setEnabled(false);
		run.setAlignmentX(JButton.RIGHT_ALIGNMENT);
		run.addActionListener(this);

		panel.add(run);

		addKeyListener(this);
		setFocusable(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();

	}

	/**
	 * Check if the parameter are good.
	 * 
	 * @return true if yes
	 */
	public boolean checkParameter() {
		boolean good = true;

		// out
		File tmp = new File(outField.getText());
		if (tmp.exists())
		{
			outPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			outPanel.setBorder(BorderFactory.createTitledBorder(outPanel.getBorder(), PARAMETER_TITLE));
			good = false;
		}

		return good;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == run)
		{
			System.out.println("confirme export");

			if (checkParameter())
			{
				System.out.println("execute export");
				manager.exportCNF(cnf, outField.getText());
				dispose();
			}
			return;
		}

		if (e.getSource() == outButton)
		{
			JFileChooser fc = new JFileChooser(lastDirectory);
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnVal = fc.showOpenDialog(null);

			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				// File file = fc.getSelectedFile();
				lastDirectory = fc.getSelectedFile();// new File(file.getParent());
				outField.setText(lastDirectory.getPath() + "/");
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
			run.doClick();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// rien
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// rien
	}

}
